package com.gz.view;

import com.gz.domain.Bill;
import com.gz.domain.DiningTable;
import com.gz.domain.Menu;
import com.gz.domain.MultiTableBean;
import com.gz.service.BillService;
import com.gz.service.DiningTableService;
import com.gz.service.EmployeeService;
import com.gz.service.MenuService;
import com.gz.utils.Utility;

import java.util.List;

public class Function {

    private DiningTableService diningTableService=new DiningTableService();
    private MenuService menuService=new MenuService();
    private BillService billService=new BillService();

    //完成订座
    public void orderDingTable(){
        System.out.println("===============预定座位===============");
        System.out.println("请选择要预定的餐桌编号(-1退出): ");
        int orderId= Utility.readInt();
        if(orderId==-1){
            System.out.println("===============取消预定座位===============");
            return;
        }
        //再确认一下
        char key=Utility.readConfirmSelection();
        if(key=='Y'){
            DiningTable diningTableById = diningTableService.getDiningTableById(orderId);
            if(diningTableById==null){
                System.out.println("===============预定座位不存在===============");
                return;
            }
            //判断座位是否为空
            if(!("空".equals(diningTableById.getState()))){
                System.out.println("===============座位已预订或就餐中===============");
                return;
            }

            //接收预定信息
            System.out.println("请输入预订人名字: ");
            String ordername=Utility.readString(50);
            System.out.println("请输入预订人电话: ");
            String orderTel=Utility.readString(50);
            //这时可以预定，更新餐桌状态
            if(diningTableService.orderDiningTable(orderId,ordername,orderTel)){
                System.out.println("===============预订成功===============");
            }
            else
                System.out.println("===============预定失败===============");

        }
        else{
            System.out.println("===============取消预定座位===============");
        }


    }

    //完成结账
    public void payBill(){
        System.out.println("================结账服务==============");
        System.out.println("请选择要结账的餐桌编号(-1退出): ");
       int diningTableId= Utility.readInt();
       if(diningTableId==-1){
           System.out.println("================取消结账==============");
           return;
       }
       //验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
       if(diningTable==null){
           System.out.println("================结账的餐桌不存在==============");
           return;
       }
        //验证餐桌是否有要结账的账单
        if(!(billService.hasPayBillByDiningTableId(diningTableId)))
        {
            System.out.println("================该餐桌没有未结账的账单==============");
            return;
        }
        System.out.println("结账方式(现金/支付宝/微信)回车表示退出: ");
        String payMode=Utility.readString(20,"");//如果回车，就是返回""
        if("".equals(payMode)){
            System.out.println("================取消结账==============");
            return;
        }
        char key=Utility.readConfirmSelection();
        if(key=='Y'){
            if (billService.payBill(diningTableId,payMode)){
                System.out.println("================结账成功==============");
            }
            else {
                System.out.println("================结账失败==============");
            }

        }else{
            System.out.println("================取消结账==============");
        }
    }

    //完成点餐
    public void orderMenu(){
        System.out.println("================点餐服务==============");
        System.out.println("请输入点餐的桌号(-1退出): ");
        int orderDiningTableId=Utility.readInt();
        if(orderDiningTableId==-1){
            System.out.println("================取消点餐==============");
            return;
        }
        System.out.println("请输入点餐的菜品号(-1退出): ");
        int orderMenuId=Utility.readInt();
        if(orderMenuId==-1){
            System.out.println("================取消点餐==============");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出): ");
        int ordernums=Utility.readInt();
        if(ordernums==-1){
            System.out.println("================取消点餐==============");
            return;
        }
        char key=Utility.readConfirmSelection();
        if(key=='Y'){
            //验证餐桌号是否存在，正常前端中有图标就存在，这里是控制台所以写一下
            DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
            if(diningTable==null){
                System.out.println("================餐桌号不存在==============");
                return;
            }
            //验证菜品号是否存在
            Menu menu=menuService.getMenuById(ordernums);
            if(menu==null){
                System.out.println("================菜品号不存在==============");
                return;
            }
            if(billService.orderMenu(orderMenuId,ordernums,orderDiningTableId)){
                System.out.println("================点餐成功==============");
            }
            else {
                System.out.println("================点餐失败==============");
            }

        }else {
            System.out.println("================取消点餐==============");
        }
    }


    //显示所有账单
    public void listBill(){
        List<MultiTableBean> list = billService.getBillList2();
        for (MultiTableBean multiTableBean : list) {
            System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t\t菜品名");
            System.out.println(multiTableBean);
        }
        System.out.println("================显示完毕==============");
    }



    //显示所有菜单
    public void listMenu(){
        List<Menu> list = menuService.list();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for(Menu menu:list){
            System.out.println(menu);
        }
        System.out.println("================显示完毕==============");
    }


    //显示所有餐桌状态
    public void listdiningTable(){
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for(DiningTable table:list){
            System.out.println(table);
        }
        System.out.println("================显示完毕==============");
    }
}
